
using Core.SimpleTemp.Common;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories.Internal;
using Core.Toolkit.AutoMapperExtend.DataDictionary;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Transactions;

namespace Core.SimpleTemp.Repositories
{
    /// <summary>
    /// /基础_隐患等级
    /// </summary>
    [AutoDi()]//注意这里 自动DI
    public class YH_GradeRepository : BaseDBRepository<YH_Grade>
    //统一继承BaseDBRepository并传入`Entity`泛型参数
    {
        public YH_GradeRepository(CoreDBContext dbContext) : base(dbContext)
        {
        }

        /// <summary>
        /// 更新部门职务关联关系
        /// </summary>
        /// <param name="GradeId">部门Id</param>
        /// <param name="ListAdd">新增集合</param>
        /// <param name="ListDel">删除集合</param>
        /// <returns></returns>
        public async Task<bool> UpdateGradePostAsync(Guid GradeId, List<YH_GradePost> ListAdd, List<YH_GradePost> ListDel)
        {

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                if (ListAdd.Count() > 0)
                {
                    List<YH_GradePost> addRangeList = new List<YH_GradePost>();
                    ListAdd.ForEach(v =>
                    {
                        var entity = _dbContext.Set<YH_GradePost>().Where(sdd => sdd.PostId == v.PostId && sdd.GradeId == v.GradeId && sdd.IsDeleted == true).FirstOrDefault();
                        if (entity != null)
                            entity.IsDeleted = false;
                        else
                            addRangeList.Add(v);
                    });
                    if (addRangeList.Count() > 0)
                        _dbContext.Set<YH_GradePost>().AddRange(addRangeList);
                }

                if (ListDel.Count() > 0)
                {
                    ListDel.ForEach(v =>
                    {
                        var entity = _dbContext.Set<YH_GradePost>().Where(sdd => sdd.Id == v.Id).FirstOrDefault();
                        if (entity != null)
                            entity.IsDeleted = true;
                    });
                }

                await _dbContext.SaveChangesAsync();
                scope.Complete();
            }
            return true;
        }

    }
}
